#include "dbconnectionui.h"
#include "ui_dbconnectionui.h"
#include <QDebug>
#include <QMessageBox>

DBConnectionUI::DBConnectionUI(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::DBConnectionUI)
{
    ui->setupUi(this);
}

DBConnectionUI::~DBConnectionUI()
{
    delete ui;
}

void DBConnectionUI::on_buttonBox_accepted()
{
    ConnectMng &conMng = ConnectMng::getInstance();
    if(conMng.getDBConnMap().contains(ui->lineEditName->text()) )
    {
        QMessageBox msgBox;
        msgBox.setText(QString("连接名%1已存在!").arg(ui->lineEditName->text()));
        msgBox.exec();
        return;
    }

    int index = ui->comboBox->currentIndex();
    if(!conMng.isSupported(index)) {
        QMessageBox msgBox;
        msgBox.setText("数据库类型暂不支持!");
        msgBox.exec();
        return;
    }

    DBConnection conn;
    conn.setName(ui->lineEditName->text());
    conn.setHost(ui->lineEditHost->text());
    conn.setUser(ui->lineEditUser->text());
    conn.setPort(ui->lineEditPort->text());
    conn.setPassword(ui->lineEditPassword->text());
    conn.setType(ui->comboBox->currentIndex());
    conMng.createConnection(conn);

    qDebug()<<conMng.getDBConnMap().size();
}

